package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RedisControllerMyRedisTemplate {
    @Autowired
    private RedisTemplate redisTemplate; //底层的时候redis定义了如果有和redisTemplate重名时用后者声明的一个

    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("redis3")
    @ResponseBody
    public String redistest3(){
        User user=new User("fyj","sb");
        redisTemplate.opsForValue().set("user",user);//存
        User user2= (User) redisTemplate.opsForValue().get("user");//取
        return user2.toString();
    }

    //自定义封装，不用每次都写opsforvalue/...了
    @RequestMapping("redis4")
    @ResponseBody
    public String redistest4(){
        User user2=new User("gm","123");
        redisUtils.set("gm",user2);
        User user= (User) redisUtils.get("gm");
        return user.toString();
    }
}
